﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Compiler.Unification.Substitutions;
using Compiler.TableOfSymbols.Types;

namespace Compiler.TableOfSymbols
{
    /// <summary>
    /// Таблица символов
    /// </summary>
    public class SymbolTable
    {
        private Dictionary<string, SymbolTableData> table;

        public SymbolTableData this[string key]
        {
            get { return table[key]; }
        }

        public SymbolTable()
        {
            table = new Dictionary<string, SymbolTableData>();
        }

        public bool Contains(string name)
        {
            if ((name == null) || (name == ""))
                throw new ArgumentOutOfRangeException("name can not be empty");
            return table.ContainsKey(name);
        }

        public void Add(string name, SymbolTableData data)
        {
            if ((name == null) || (name == ""))
                throw new ArgumentOutOfRangeException("name can not be empty");
            if (data == null)
                throw new ArgumentNullException("data can not be null");
            table.Add(name, data);
        }

        public void ApplySubstitution(Substitution subs)
        {
            var keys = new LinkedList<string>(table.Keys);
            foreach (string name in keys)
                if (table[name].Category == SymbolTableDataCategory.TYPE)
                    table[name] = subs.Apply(table[name] as BaseType);
        }
    }
}
